package com.study.Util;

import com.study.common.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

@Slf4j
public class ASEUtil {

    /**
     * 解密
     *
     * @param key1
     * @return
     */
    public static String decry64(String key1) {
        if (StringUtils.isEmpty(key1)) {
            return null;
        }
        try {
            byte[] bytes = (new BASE64Decoder()).decodeBuffer(key1);
            return new String(bytes);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException("解密失败");
        }

    }

    /***
     * BASE64加密
     * @return
     * @throws Exception
     */
    public static String encrypt64(String s) {
        if (StringUtils.isEmpty(s)) {
            return null;
        }
        byte[] bytes = s.getBytes();
        try {
            return (new BASE64Encoder()).encode(bytes);
        } catch (Exception e) {
            throw new BusinessException("加密失败");
        }
    }
}
